using System;
using CLELCore.Collections;

namespace CLELCore.Assembly.BlobStreams
{
	public class BlobStream
	{
		private Heap _blobs;
		private int _size;

		public int RawSize
		{
			get
			{
				return _blobs.Size;
			}
		}
		public int Size
		{
			get
			{
				return _size;
			}
		}

		public BlobStream()
		{
			_blobs = new Heap();
			_size = 0;
		}

		public int Add(Blob blob)
		{
			if(blob == null)
				throw new Exception("Error blob cannot be null");
			int off = _blobs.Malloc(blob.Length);
			byte[] temp = blob.BlobValue;
			_size += blob.Length;
			_blobs.Set(off,temp);
			return off;
		}

		public bool Set(int offset,byte [] val)
		{
			return _blobs.Set(offset,val);
		}

		public int Resize(int offset,int length)
		{
			Blob b = Get(offset);
			byte [] v = b.BlobValue;
			_size -= v.Length;
			_size += length;
			return _blobs.Resize(offset,length);
		}

		public void Free(int off)
		{
			_blobs.Free(off);
		}

		public int[][] Compress()
		{
			//TODO: RESET _SIZE AND UPDATE IT
			return _blobs.Compress();
		}

		public Blob Get(int off)
		{
			byte[] bytes = _blobs.Get(off);
			Blob b = Blob.ParseBlob(bytes,0);
			return b;
		}

		public byte[] GetBytes(int off)
		{
			return _blobs.Get(off);
		}
	}
}
